package com.tenglong.teacher.ui.setting.setting;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddFeedbackActivity extends BaseActivity implements TextWatcher {

    private EditText feedback;
    private TextView countNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_feedback);
        initView();
        setListeners();
    }

    private void setListeners() {
        feedback.addTextChangedListener(this);
        findViewById(R.id.tvCommit).setOnClickListener(this);
    }

    private void initView() {
        setTitle("用户反馈");
        feedback = findViewById(R.id.feedback);
        countNum = findViewById(R.id.countNum);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        countNum.setText(s.toString().trim().length() + "/" + 140);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tvCommit:
                if (TextUtils.isEmpty(feedback.getText().toString().trim())) {
                    ToastUtils.showCenterToast("用户反馈不能为空");
                    return;
                }
                try {
                    request();
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void request() throws PackageManager.NameNotFoundException {
        int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
        RequestBody text = RequestBody.create(MediaType.parse("text/plain"), feedback.getText().toString().trim());
        CreateRequestEntity.getWebService().feedback(versionCode, text).enqueue(new Callback<BaseResult<Boolean>>() {
            @Override
            public void onResponse(Call<BaseResult<Boolean>> call, Response<BaseResult<Boolean>> response) {
                if (ResultVerifier.isSucceed(response)) {
                    ToastUtils.showCenterToast("反馈成功");
                    finish();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
            }

            @Override
            public void onFailure(Call<BaseResult<Boolean>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
            }
        });
    }
}
